using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class ProtectedPrefs : MonoBehaviour
{
	public static ProtectedPrefs instance;

	public static string KeyCrypt = "_crypt";

	public string pass = string.Empty;

	private static HMACSHA1 hmacsha1;

	private static byte[] salt;

	private static string IdDevice
	{
		get
		{
			return SystemInfo.deviceUniqueIdentifier;
		}
	}

	private void Awake()
	{
		instance = this;
		if (salt == null)
		{
			salt = GetSalt();
		}
		if (hmacsha1 == null)
		{
			hmacsha1 = GetSHA1(instance.pass, salt);
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private static byte[] GetSalt()
	{
		byte[] bytes = Encoding.UTF8.GetBytes(IdDevice);
		if (bytes.Length < 8)
		{
			bytes = Encoding.UTF8.GetBytes(IdDevice + "secretKey");
		}
		return bytes;
	}

	private static HMACSHA1 GetSHA1(string password, byte[] curSalt)
	{
		Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, curSalt);
		byte[] bytes = rfc2898DeriveBytes.GetBytes(64);
		return new HMACSHA1(bytes, true);
	}

	private static string MyCrypt(string input)
	{
		if (salt == null)
		{
			salt = GetSalt();
		}
		if (hmacsha1 == null)
		{
			hmacsha1 = GetSHA1(instance.pass, salt);
		}
		byte[] array = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(input));
		StringBuilder stringBuilder = new StringBuilder();
		byte[] array2 = array;
		foreach (byte b in array2)
		{
			stringBuilder.AppendFormat("{0:x2}", b);
		}
		return stringBuilder.ToString();
	}

	private static string CryptoKey(string key, string val)
	{
		string input = key + val;
		return MyCrypt(input);
	}

	public static bool CryptoKeyIntCorrect(string key)
	{
		string text = Load.LoadString(key + KeyCrypt);
		string text2 = CryptoKey(key, Load.LoadInt(key).ToString());
		return text2 == text;
	}

	public static bool CryptoKeyFloatCorrect(string key)
	{
		string text = Load.LoadString(key + KeyCrypt);
		string text2 = CryptoKey(key, Load.LoadFloat(key).ToString());
		return text2 == text;
	}

	public static bool CryptoKeyBoolCorrect(string key)
	{
		string text = Load.LoadString(key + KeyCrypt);
		if (string.IsNullOrEmpty(text))
		{
			return false;
		}
		string text2 = CryptoKey(key, Load.LoadBool(key).ToString());
		return text2 == text;
	}

	public static bool CryptoKeyStringCorrect(string key)
	{
		string text = Load.LoadString(key + KeyCrypt);
		if (string.IsNullOrEmpty(text))
		{
			return false;
		}
		string text2 = CryptoKey(key, Load.LoadString(key));
		return text2 == text;
	}

	public static void SaveInt(string key, int val)
	{
		string variable = CryptoKey(key, val.ToString());
		Save.SaveInt(key, val);
		Save.SaveString(key + KeyCrypt, variable);
	}

	public static void SaveFloat(string key, float val)
	{
		string variable = CryptoKey(key, val.ToString());
		Save.SaveFloat(key, val);
		Save.SaveString(key + KeyCrypt, variable);
	}

	public static void SaveBool(string key, bool val)
	{
		string variable = CryptoKey(key, val.ToString());
		Save.SaveBool(key, val);
		Save.SaveString(key + KeyCrypt, variable);
	}

	public static void SaveString(string key, string val)
	{
		string variable = CryptoKey(key, val);
		Save.SaveString(key, val);
		Save.SaveString(key + KeyCrypt, variable);
	}
}
